<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Docker-compose | 静思田园</title>
    <meta name="description" content="时不时记录一点点">
    <meta name="generator" content="VitePress v1.5.0">
    <link rel="preload stylesheet" href="/assets/style.D1KHeDuB.css" as="style">
    <link rel="preload stylesheet" href="/vp-icons.css" as="style">
    <script type="module" src="/assets/chunks/metadata.d4b76d32.js"></script>
    <script type="module" src="/assets/app.Cqoht_Of.js"></script>
    <link rel="preload" href="/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/assets/chunks/framework.trnLO_3_.js">
    <link rel="modulepreload" href="/assets/chunks/theme.BNMYWj-o.js">
    <link rel="modulepreload" href="/assets/chunks/katex.BZy9Y_85.js">
    <link rel="modulepreload" href="/assets/chunks/c4Diagram-3d4e48cf.rhyfUO3k.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-66a62f08.BvVfKYLl.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-v2-96b9c2cf.rYAPEXyw.js">
    <link rel="modulepreload" href="/assets/chunks/erDiagram-9861fffd.CQSbaoJM.js">
    <link rel="modulepreload" href="/assets/chunks/gitGraphDiagram-72cf32ee.CjLmC8RV.js">
    <link rel="modulepreload" href="/assets/chunks/ganttDiagram-c361ad54.R_V-FeK5.js">
    <link rel="modulepreload" href="/assets/chunks/infoDiagram-f8f76790.C-2Va9KZ.js">
    <link rel="modulepreload" href="/assets/chunks/pieDiagram-8a3498a8.B9ZVkD7K.js">
    <link rel="modulepreload" href="/assets/chunks/quadrantDiagram-120e2f19.BuonnDHi.js">
    <link rel="modulepreload" href="/assets/chunks/xychartDiagram-e933f94c.C_bIYaUn.js">
    <link rel="modulepreload" href="/assets/chunks/requirementDiagram-deff3bca.RFVzvdjl.js">
    <link rel="modulepreload" href="/assets/chunks/sequenceDiagram-704730f1.UZWxMlyq.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-70f12bd4.sipmPMFa.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-v2-f2320105.CDkLxQ0T.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-587899a1.CD2ocVZB.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-v2-d93cdb3a.D0qUPm3J.js">
    <link rel="modulepreload" href="/assets/chunks/journeyDiagram-49397b02.CeUUZA2l.js">
    <link rel="modulepreload" href="/assets/chunks/flowchart-elk-definition-4a651766.DkdgpMTf.js">
    <link rel="modulepreload" href="/assets/chunks/timeline-definition-85554ec2.WL9yxus9.js">
    <link rel="modulepreload" href="/assets/chunks/mindmap-definition-fc14e90a.CWC2bxmB.js">
    <link rel="modulepreload" href="/assets/chunks/sankeyDiagram-04a897e0.BXj8OViR.js">
    <link rel="modulepreload" href="/assets/chunks/blockDiagram-38ab4fdb.CPVnJXk5.js">
    <link rel="modulepreload" href="/assets/chunks/virtual_mermaid-config.DDnGl6nM.js">
    <link rel="modulepreload" href="/assets/docker_compose.md.CRnY7mBf.lean.js">
    <meta name="application-name" content="VuePress">
    <meta name="apple-mobile-web-app-title" content="静思田园">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileColor" content="#3eaf7c">
    <meta name="theme-color" content="#646cff">
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
    <script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0f60ec36></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0f60ec36> Skip to content </a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar" data-v-ae24b3ad data-v-6aa21345><div class="wrapper" data-v-6aa21345><div class="container" data-v-6aa21345><div class="title" data-v-6aa21345><div class="VPNavBarTitle has-sidebar" data-v-6aa21345 data-v-1168a8e4><a class="title" href="/" data-v-1168a8e4><!--[--><!--]--><!--[--><img class="VPImage logo" src="/logo.png" alt data-v-8426fc1a><!--]--><span data-v-1168a8e4>静思田园</span><!--[--><!--]--></a></div></div><div class="content" data-v-6aa21345><div class="content-body" data-v-6aa21345><!--[--><!--]--><div class="VPNavBarSearch search" data-v-6aa21345><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="搜索文档"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">搜索文档</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-6aa21345 data-v-dc692963><span id="main-nav-aria-label" class="visually-hidden" data-v-dc692963> Main Navigation </span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57></span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/network/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>网络工程师</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/frontend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>前端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/English/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>English</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Scrupy/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>爬虫</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/backend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>后端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/bigdata/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>大数据</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/computer/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>计算机基础</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/database/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>数据库</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/docker/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>dorker</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/git/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Git</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/nodejs/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Node.js</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/python/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Python</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/shell/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>shell</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/tools/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>工具</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-6aa21345 data-v-6c893767><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-6c893767 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-6aa21345 data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-6aa21345 data-v-bb2aa2f0 data-v-cf11d7a2><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-cf11d7a2><span class="vpi-more-horizontal icon" data-v-cf11d7a2></span></button><div class="menu" data-v-cf11d7a2><div class="VPMenu" data-v-cf11d7a2 data-v-b98bc113><!----><!--[--><!--[--><!----><div class="group" data-v-bb2aa2f0><div class="item appearance" data-v-bb2aa2f0><p class="label" data-v-bb2aa2f0>Appearance</p><div class="appearance-action" data-v-bb2aa2f0><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-bb2aa2f0 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div></div></div><div class="group" data-v-bb2aa2f0><div class="item social-links" data-v-bb2aa2f0><div class="VPSocialLinks social-links-list" data-v-bb2aa2f0 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-6aa21345 data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><div class="divider" data-v-6aa21345><div class="divider-line" data-v-6aa21345></div></div></div><!----></header><div class="VPLocalNav has-sidebar empty" data-v-5d98c3a5 data-v-a6f0e41e><div class="container" data-v-a6f0e41e><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-a6f0e41e><span class="vpi-align-left menu-icon" data-v-a6f0e41e></span><span class="menu-text" data-v-a6f0e41e>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-a6f0e41e data-v-17a5e62e><button data-v-17a5e62e>Return to top</button><!----></div></div></div><aside class="VPSidebar" data-v-5d98c3a5 data-v-319d5ca6><div class="curtain" data-v-319d5ca6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-319d5ca6><span class="visually-hidden" id="sidebar-aria-label" data-v-319d5ca6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="no-transition group" data-v-c40bc020><section class="VPSidebarItem level-0 has-active" data-v-c40bc020 data-v-b7550ba0><div class="item" role="button" tabindex="0" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><h2 class="text" data-v-b7550ba0>dorker</h2><!----></div><div class="items" data-v-b7550ba0><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>dorker</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/Dockerfile.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Dockerfile</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/compose.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker-compose</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/concept.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker 基础概念</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/container.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>container</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/images.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>images</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/mac-docker-auto.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Mac下给docker配置命令补全</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/multi-architecture.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker 的多架构支持</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/network.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker 网络</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/security.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>安全</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/storate.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>存储Volume &amp; Bind mount</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/swarm.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Swarm</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-5d98c3a5 data-v-1428d186><div class="VPDoc has-sidebar has-aside" data-v-1428d186 data-v-39a288b8><!--[--><!--]--><div class="container" data-v-39a288b8><div class="aside" data-v-39a288b8><div class="aside-curtain" data-v-39a288b8></div><div class="aside-container" data-v-39a288b8><div class="aside-content" data-v-39a288b8><div class="VPDocAside" data-v-39a288b8 data-v-3f215769><!--[--><!--]--><!--[--><!--]--><nav aria-labelledby="doc-outline-aria-label" class="VPDocAsideOutline" data-v-3f215769 data-v-a5bbad30><div class="content" data-v-a5bbad30><div class="outline-marker" data-v-a5bbad30></div><div aria-level="2" class="outline-title" id="doc-outline-aria-label" role="heading" data-v-a5bbad30>目录</div><ul class="VPDocOutlineItem root" data-v-a5bbad30 data-v-b933a997><!--[--><!--]--></ul></div></nav><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-39a288b8><div class="content-container" data-v-39a288b8><!--[--><!--]--><main class="main" data-v-39a288b8><div style="position:relative;" class="vp-doc _docker_compose" data-v-39a288b8><div><h1 id="docker-compose" tabindex="-1">Docker-compose <a class="header-anchor" href="#docker-compose" aria-label="Permalink to &quot;Docker-compose&quot;">​</a></h1><table tabindex="0"><thead><tr><th>网址</th><th>备注</th></tr></thead><tbody><tr><td><a href="https://github.com/docker/awesome-compose?tab=readme-ov-file" target="_blank" rel="noreferrer">优秀示例</a></td><td></td></tr></tbody></table><h2 id="安装" tabindex="-1">安装 <a class="header-anchor" href="#安装" aria-label="Permalink to &quot;安装&quot;">​</a></h2><p>Windows和Mac在默认安装了docker desktop以后，docker-compose随之自动安装.</p><p>Linux用户需要自行<a href="https://github.com/docker/compose/releases" target="_blank" rel="noreferrer">安装</a></p><p>直接安装：</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># version=v2.27.1</span></span>
<span class="line"><span style="color:#61AFEF;">$</span><span style="color:#98C379;"> sudo</span><span style="color:#98C379;"> curl</span><span style="color:#D19A66;"> -L</span><span style="color:#98C379;"> &quot;https://github.com/docker/compose/releases/download/&lt;version&gt;/docker-compose-$(</span><span style="color:#61AFEF;">uname</span><span style="color:#D19A66;"> -s</span><span style="color:#98C379;">)-$(</span><span style="color:#61AFEF;">uname</span><span style="color:#D19A66;"> -m</span><span style="color:#98C379;">)&quot;</span><span style="color:#D19A66;"> -o</span><span style="color:#98C379;"> /usr/local/bin/docker-compose</span></span>
<span class="line"><span style="color:#61AFEF;">$</span><span style="color:#98C379;"> sudo</span><span style="color:#98C379;"> chmod</span><span style="color:#98C379;"> +x</span><span style="color:#98C379;"> /usr/local/bin/docker-compose</span></span>
<span class="line"><span style="color:#61AFEF;">$</span><span style="color:#98C379;"> docker-compose</span><span style="color:#D19A66;"> --version</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>pip去安装docker-Compose:</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">$</span><span style="color:#98C379;"> pip</span><span style="color:#98C379;"> install</span><span style="color:#98C379;"> docker-compose</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><h2 id="语法" tabindex="-1">语法 <a class="header-anchor" href="#语法" aria-label="Permalink to &quot;语法&quot;">​</a></h2><h3 id="基本语法" tabindex="-1">基本<a href="https://docs.docker.com/compose/compose-file/" target="_blank" rel="noreferrer">语法</a> <a class="header-anchor" href="#基本语法" aria-label="Permalink to &quot;基本[语法](https://docs.docker.com/compose/compose-file/)&quot;">​</a></h3><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&quot;3.8&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">: </span><span style="color:#7F848E;font-style:italic;"># 一个或者多个容器</span></span>
<span class="line"><span style="color:#E06C75;">  servicename</span><span style="color:#ABB2BF;">: </span><span style="color:#7F848E;font-style:italic;"># 服务名字，这个名字也是内部 bridge网络可以使用的 DNS name</span></span>
<span class="line"><span style="color:#E06C75;">  	container_name</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">new-container-name</span><span style="color:#7F848E;font-style:italic;"> # 容器名字，默认为服务名字，同时会忽略项目名字和数字</span></span>
<span class="line"><span style="color:#E06C75;">    image</span><span style="color:#ABB2BF;">: </span><span style="color:#7F848E;font-style:italic;"># 镜像的名字</span></span>
<span class="line"><span style="color:#E06C75;">    command</span><span style="color:#ABB2BF;">: </span><span style="color:#7F848E;font-style:italic;"># 可选，如果设置，则会覆盖默认镜像里的 CMD命令</span></span>
<span class="line"><span style="color:#E06C75;">    environment</span><span style="color:#ABB2BF;">: </span><span style="color:#7F848E;font-style:italic;"># 可选，相当于 docker run里的 --env</span></span>
<span class="line"><span style="color:#E06C75;">    volumes</span><span style="color:#ABB2BF;">: </span><span style="color:#7F848E;font-style:italic;"># 可选，相当于docker run里的 -v</span></span>
<span class="line"><span style="color:#E06C75;">    networks</span><span style="color:#ABB2BF;">: </span><span style="color:#7F848E;font-style:italic;"># 可选，相当于 docker run里的 --network</span></span>
<span class="line"><span style="color:#E06C75;">    ports</span><span style="color:#ABB2BF;">: </span><span style="color:#7F848E;font-style:italic;"># 可选，相当于 docker run里的 -p</span></span>
<span class="line"><span style="color:#E06C75;">  servicename2</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">volumes</span><span style="color:#ABB2BF;">: </span><span style="color:#7F848E;font-style:italic;"># 可选，相当于 docker volume create</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">networks</span><span style="color:#ABB2BF;">: </span><span style="color:#7F848E;font-style:italic;"># 可选，相当于 docker network create</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="示例" tabindex="-1">示例： <a class="header-anchor" href="#示例" aria-label="Permalink to &quot;示例：&quot;">​</a></h3><h4 id="app-py" tabindex="-1">app.py <a class="header-anchor" href="#app-py" aria-label="Permalink to &quot;app.py&quot;">​</a></h4><div class="language-python line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">from</span><span style="color:#ABB2BF;"> flask </span><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> Flask</span></span>
<span class="line"><span style="color:#C678DD;">from</span><span style="color:#ABB2BF;"> redis </span><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> Redis</span></span>
<span class="line"><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> os</span></span>
<span class="line"><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> socket</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">app </span><span style="color:#56B6C2;">=</span><span style="color:#61AFEF;"> Flask</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">__name__</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">redis </span><span style="color:#56B6C2;">=</span><span style="color:#61AFEF;"> Redis</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">host</span><span style="color:#56B6C2;">=</span><span style="color:#ABB2BF;">os.environ.</span><span style="color:#61AFEF;">get</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;REDIS_HOST&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&#39;127.0.0.1&#39;</span><span style="color:#ABB2BF;">), </span><span style="color:#E06C75;font-style:italic;">port</span><span style="color:#56B6C2;">=</span><span style="color:#D19A66;">6379</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">@app</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">route</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;/&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">def</span><span style="color:#61AFEF;"> hello</span><span style="color:#ABB2BF;">():</span></span>
<span class="line"><span style="color:#ABB2BF;">    redis.</span><span style="color:#61AFEF;">incr</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;hits&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">    return</span><span style="color:#C678DD;"> f</span><span style="color:#98C379;">&quot;Hello Container World! I have been seen </span><span style="color:#D19A66;">{</span><span style="color:#ABB2BF;">redis.</span><span style="color:#61AFEF;">get</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;hits&#39;</span><span style="color:#ABB2BF;">).</span><span style="color:#61AFEF;">decode</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;utf-8&#39;</span><span style="color:#ABB2BF;">)</span><span style="color:#D19A66;">}</span><span style="color:#98C379;"> times and my hostname is </span><span style="color:#D19A66;">{</span><span style="color:#ABB2BF;">socket.</span><span style="color:#61AFEF;">gethostname</span><span style="color:#ABB2BF;">()</span><span style="color:#D19A66;">}</span><span style="color:#98C379;">.</span><span style="color:#56B6C2;">\n</span><span style="color:#98C379;">&quot;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>Dockerfile：</p><div class="language-dockerfile line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">dockerfile</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">FROM</span><span style="color:#ABB2BF;"> python:3.9.5-slim</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">RUN</span><span style="color:#ABB2BF;"> pip install flask redis &amp;&amp; \</span></span>
<span class="line"><span style="color:#ABB2BF;">    groupadd -r flask &amp;&amp; useradd -r -g flask flask &amp;&amp; \</span></span>
<span class="line"><span style="color:#ABB2BF;">    mkdir /src &amp;&amp; \</span></span>
<span class="line"><span style="color:#ABB2BF;">    chown -R flask:flask /src</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">USER</span><span style="color:#ABB2BF;"> flask</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">COPY</span><span style="color:#ABB2BF;"> app.py /src/app.py</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">WORKDIR</span><span style="color:#ABB2BF;"> /src</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">ENV</span><span style="color:#ABB2BF;"> FLASK_APP=app.py REDIS_HOST=redis</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">EXPOSE</span><span style="color:#ABB2BF;"> 5000</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">CMD</span><span style="color:#ABB2BF;"> [</span><span style="color:#98C379;">&quot;flask&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&quot;run&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&quot;-h&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&quot;0.0.0.0&quot;</span><span style="color:#ABB2BF;">]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h4 id="docker-compose-yml-文件如下" tabindex="-1">docker-compose.yml 文件如下 <a class="header-anchor" href="#docker-compose-yml-文件如下" aria-label="Permalink to &quot;docker-compose.yml 文件如下&quot;">​</a></h4><h5 id="version-1" tabindex="-1">version 1 <a class="header-anchor" href="#version-1" aria-label="Permalink to &quot;version 1&quot;">​</a></h5><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&#39;3.8&#39;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    redis-server</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">redis:latest</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">demo-network</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">    flask-demo</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">flask-demo:latest</span></span>
<span class="line"><span style="color:#E06C75;">        environment</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">REDIS_HOST=redis-server</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">demo-network</span></span>
<span class="line"><span style="color:#E06C75;">        ports</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">8080:5000</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    demo-network</span><span style="color:#ABB2BF;">:</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h5 id="version-2" tabindex="-1">version 2 <a class="header-anchor" href="#version-2" aria-label="Permalink to &quot;version 2&quot;">​</a></h5><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&#39;3.8&#39;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    redis-server</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">redis:latest</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">demo-network</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">    flask-demo</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        container_name</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">my-flask-demo</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">flask-demo:latest</span></span>
<span class="line"><span style="color:#E06C75;">        environment</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">REDIS_HOST=redis-server</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">demo-network</span></span>
<span class="line"><span style="color:#E06C75;">        ports</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">8080:5000</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    demo-network</span><span style="color:#ABB2BF;">:</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h5 id="version-3" tabindex="-1">Version 3 <a class="header-anchor" href="#version-3" aria-label="Permalink to &quot;Version 3&quot;">​</a></h5><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&#39;3.8&#39;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    redis-server</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">redis:latest</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">demo-network</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">    flask-demo</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        build</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&#39;./flask&#39;</span></span>
<span class="line"><span style="color:#E06C75;">        container_name</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">my-flask-demo</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">flask-demo:latest</span></span>
<span class="line"><span style="color:#E06C75;">        environment</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">REDIS_HOST=redis-server</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">demo-network</span></span>
<span class="line"><span style="color:#E06C75;">        ports</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">8080:5000</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    demo-network</span><span style="color:#ABB2BF;">:</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h5 id="version-4" tabindex="-1">Version 4 <a class="header-anchor" href="#version-4" aria-label="Permalink to &quot;Version 4&quot;">​</a></h5><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&#39;3.8&#39;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    redis-server</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">redis:latest</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">demo-network</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">    flask-demo</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        build</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">            context</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">./flask</span></span>
<span class="line"><span style="color:#E06C75;">            dockerfile</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">Dockerfile</span></span>
<span class="line"><span style="color:#E06C75;">        container_name</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">my-flask-demo</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">flask-demo:latest</span></span>
<span class="line"><span style="color:#E06C75;">        environment</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">REDIS_HOST=redis-server</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">demo-network</span></span>
<span class="line"><span style="color:#E06C75;">        ports</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">8080:5000</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    demo-network</span><span style="color:#ABB2BF;">:</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h4 id="启动" tabindex="-1">启动 <a class="header-anchor" href="#启动" aria-label="Permalink to &quot;启动&quot;">​</a></h4><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># 前台服务运行</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> up</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 后台运行</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> up</span><span style="color:#D19A66;"> -d</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># rebuild</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> up</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --build</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># remove not used</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> up</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --remove-orphans</span><span style="color:#D19A66;"> --build</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 重启服务</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> restart</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 停止服务</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> stop</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 移除并停止服务</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> down</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 移除停止的服务</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> rm</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># docker-compose 启动的服务，默认会加上文件夹的名字作为前缀，也可以通过 -p 指定服务名称前缀，并且以后所有相关操作都需要加 -p 选项</span></span>
<span class="line"><span style="color:#61AFEF;">docker-composer</span><span style="color:#D19A66;"> -p</span><span style="color:#98C379;"> myproject</span><span style="color:#98C379;"> up</span><span style="color:#D19A66;"> -d</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 查看变量的真实值,默认是.env,自定义的env需紧跟docker-compose后，包括config, up 命令全是需要要紧跟</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#D19A66;"> --env-file</span><span style="color:#98C379;"> ./myenv</span><span style="color:#98C379;"> config</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><h2 id="网络" tabindex="-1">网络 <a class="header-anchor" href="#网络" aria-label="Permalink to &quot;网络&quot;">​</a></h2><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&#39;3.8&#39;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    box1</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">xiaopeng163/net-box:latest</span></span>
<span class="line"><span style="color:#E06C75;">        command</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">/bin/sh -c &quot;while true; do sleep 3600; done&quot;</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">mynetwork1</span></span>
<span class="line"><span style="color:#E06C75;">    box2</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">xiaopeng163/net-box:latest</span></span>
<span class="line"><span style="color:#E06C75;">        command</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">/bin/sh -c &quot;while true; do sleep 3600; done&quot;</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">mynetwork1</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">mynetwork2</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    mynetwork1</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    mynetwork2</span><span style="color:#ABB2BF;">:</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h2 id="水平扩展" tabindex="-1">水平扩展 <a class="header-anchor" href="#水平扩展" aria-label="Permalink to &quot;水平扩展&quot;">​</a></h2><p>docker-compose.yaml</p><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&#39;3.8&#39;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    flask</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        build</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">            context</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">./flask</span></span>
<span class="line"><span style="color:#E06C75;">            dockerfile</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">Dockerfile</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">flask-demo:latest</span></span>
<span class="line"><span style="color:#E06C75;">        environment</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">REDIS_HOST=redis-server</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">    redis-server</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">redis:latest</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">    client</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">xiaopeng163/net-box:latest</span></span>
<span class="line"><span style="color:#E06C75;">        command</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">sh -c &quot;while true; do sleep 3600; done;&quot;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h4 id="测试" tabindex="-1">测试 <a class="header-anchor" href="#测试" aria-label="Permalink to &quot;测试&quot;">​</a></h4><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># scale, load balance</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> up</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --scale</span><span style="color:#98C379;"> flask=</span><span style="color:#D19A66;">3</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># cut short</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> up</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --scale</span><span style="color:#98C379;"> flask=</span><span style="color:#D19A66;">1</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="nginx" tabindex="-1">Nginx <a class="header-anchor" href="#nginx" aria-label="Permalink to &quot;Nginx&quot;">​</a></h3><p>docker-compose.yml</p><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&quot;3.8&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">  flask</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    build</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">      context</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">./flask</span></span>
<span class="line"><span style="color:#E06C75;">      dockerfile</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">Dockerfile</span></span>
<span class="line"><span style="color:#E06C75;">    image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">flask-demo:latest</span></span>
<span class="line"><span style="color:#E06C75;">    environment</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">REDIS_HOST=redis-server</span></span>
<span class="line"><span style="color:#E06C75;">    networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">backend</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">frontend</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">  redis-server</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">redis:latest</span></span>
<span class="line"><span style="color:#E06C75;">    networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">backend</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">  nginx</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">nginx:stable-alpine</span></span>
<span class="line"><span style="color:#E06C75;">    ports</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">8000:80</span></span>
<span class="line"><span style="color:#E06C75;">    depends_on</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">flask</span></span>
<span class="line"><span style="color:#E06C75;">    volumes</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro</span><span style="color:#7F848E;font-style:italic;"> # read only</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">./var/log/nginx:/var/log/nginx</span></span>
<span class="line"><span style="color:#E06C75;">    networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">frontend</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">  backend</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">  frontend</span><span style="color:#ABB2BF;">:</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><p>nginx.conf:</p><div class="language-nginx line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">nginx</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">server</span><span style="color:#ABB2BF;"> {</span></span>
<span class="line"><span style="color:#C678DD;">  listen </span><span style="color:#D19A66;"> 80</span><span style="color:#D19A66;"> default_server</span><span style="color:#ABB2BF;">;</span></span>
<span class="line"><span style="color:#C678DD;">  location</span><span style="color:#ABB2BF;"> / {</span></span>
<span class="line"><span style="color:#C678DD;">    proxy_pass </span><span style="color:#ABB2BF;">http://flask:5000;</span></span>
<span class="line"><span style="color:#ABB2BF;">  }</span></span>
<span class="line"><span style="color:#ABB2BF;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>测试同上，注意重启nginx <code>docker-compose restart nginx</code>。</p><h2 id="环境变量" tabindex="-1">环境<a href="https://docs.docker.com/compose/environment-variables/" target="_blank" rel="noreferrer">变量</a> <a class="header-anchor" href="#环境变量" aria-label="Permalink to &quot;环境[变量](https://docs.docker.com/compose/environment-variables/)&quot;">​</a></h2><p>app.py</p><div class="language-py line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">py</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">from</span><span style="color:#ABB2BF;"> flask </span><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> Flask</span></span>
<span class="line"><span style="color:#C678DD;">from</span><span style="color:#ABB2BF;"> redis </span><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> StrictRedis</span></span>
<span class="line"><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> os</span></span>
<span class="line"><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> socket</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">app </span><span style="color:#56B6C2;">=</span><span style="color:#61AFEF;"> Flask</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">__name__</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">redis </span><span style="color:#56B6C2;">=</span><span style="color:#61AFEF;"> StrictRedis</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">host</span><span style="color:#56B6C2;">=</span><span style="color:#ABB2BF;">os.environ.</span><span style="color:#61AFEF;">get</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;REDIS_HOST&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&#39;127.0.0.1&#39;</span><span style="color:#ABB2BF;">),</span></span>
<span class="line"><span style="color:#E06C75;font-style:italic;">                    port</span><span style="color:#56B6C2;">=</span><span style="color:#D19A66;">6379</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">password</span><span style="color:#56B6C2;">=</span><span style="color:#ABB2BF;">os.environ.</span><span style="color:#61AFEF;">get</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;REDIS_PASS&#39;</span><span style="color:#ABB2BF;">))</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">@app</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">route</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;/&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">def</span><span style="color:#61AFEF;"> hello</span><span style="color:#ABB2BF;">():</span></span>
<span class="line"><span style="color:#ABB2BF;">    redis.</span><span style="color:#61AFEF;">incr</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;hits&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">    return</span><span style="color:#C678DD;"> f</span><span style="color:#98C379;">&quot;Hello Container World! I have been seen </span><span style="color:#D19A66;">{</span><span style="color:#ABB2BF;">redis.</span><span style="color:#61AFEF;">get</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;hits&#39;</span><span style="color:#ABB2BF;">).</span><span style="color:#61AFEF;">decode</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;utf-8&#39;</span><span style="color:#ABB2BF;">)</span><span style="color:#D19A66;">}</span><span style="color:#98C379;"> times and my hostname is </span><span style="color:#D19A66;">{</span><span style="color:#ABB2BF;">socket.</span><span style="color:#61AFEF;">gethostname</span><span style="color:#ABB2BF;">()</span><span style="color:#D19A66;">}</span><span style="color:#98C379;">.</span><span style="color:#56B6C2;">\n</span><span style="color:#98C379;">&quot;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>docker-compose.yml</p><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&quot;3.8&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">  flask</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    build</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">      context</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">./flask</span></span>
<span class="line"><span style="color:#E06C75;">      dockerfile</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">Dockerfile</span></span>
<span class="line"><span style="color:#E06C75;">    image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">flask-demo:latest</span></span>
<span class="line"><span style="color:#E06C75;">    environment</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">REDIS_HOST=redis-server</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">REDIS_PASS=${REDIS_PASSWORD}</span></span>
<span class="line"><span style="color:#E06C75;">    networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">backend</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">frontend</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">  redis-server</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">redis:latest</span></span>
<span class="line"><span style="color:#E06C75;">    command</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">redis-server --requirepass ${REDIS_PASSWORD}</span></span>
<span class="line"><span style="color:#E06C75;">    networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">backend</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">  nginx</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">nginx:stable-alpine</span></span>
<span class="line"><span style="color:#E06C75;">    ports</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">8000:80</span></span>
<span class="line"><span style="color:#E06C75;">    depends_on</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">flask</span></span>
<span class="line"><span style="color:#E06C75;">    volumes</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">./var/log/nginx:/var/log/nginx</span></span>
<span class="line"><span style="color:#E06C75;">    networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">      - </span><span style="color:#98C379;">frontend</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">  backend</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">  frontend</span><span style="color:#ABB2BF;">:</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div><p>其它文件nginx同上。</p><h2 id="服务依赖和健康检查" tabindex="-1">服务依赖和健康<a href="https://docs.docker.com/engine/reference/builder/#healthcheck" target="_blank" rel="noreferrer">检查</a> <a class="header-anchor" href="#服务依赖和健康检查" aria-label="Permalink to &quot;服务依赖和健康[检查](https://docs.docker.com/engine/reference/builder/#healthcheck)&quot;">​</a></h2><h3 id="简易演示" tabindex="-1">简易演示 <a class="header-anchor" href="#简易演示" aria-label="Permalink to &quot;简易演示&quot;">​</a></h3><p>Dockerfile:</p><div class="language-dockerfile line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">dockerfile</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">FROM</span><span style="color:#ABB2BF;"> python:3.9.5-slim</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">RUN</span><span style="color:#ABB2BF;"> pip install flask redis &amp;&amp; \</span></span>
<span class="line"><span style="color:#ABB2BF;">    apt-get update &amp;&amp; \</span></span>
<span class="line"><span style="color:#ABB2BF;">    apt-get install -y curl &amp;&amp; \</span></span>
<span class="line"><span style="color:#ABB2BF;">    groupadd -r flask &amp;&amp; useradd -r -g flask flask &amp;&amp; \</span></span>
<span class="line"><span style="color:#ABB2BF;">    mkdir /src &amp;&amp; \</span></span>
<span class="line"><span style="color:#ABB2BF;">    chown -R flask:flask /src</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">USER</span><span style="color:#ABB2BF;"> flask</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">COPY</span><span style="color:#ABB2BF;"> app.py /src/app.py</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">WORKDIR</span><span style="color:#ABB2BF;"> /src</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">ENV</span><span style="color:#ABB2BF;"> FLASK=app.py REDIS_HOST=redis</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">EXPOSE</span><span style="color:#ABB2BF;"> 5000</span></span>
<span class="line"></span>
<span class="line highlighted"><span style="color:#61AFEF;">HEALTHCHECK</span><span style="color:#ABB2BF;"> --interval=30s \</span></span>
<span class="line highlighted"><span style="color:#ABB2BF;">            --timeout=3s \</span></span>
<span class="line highlighted"><span style="color:#61AFEF;">            CMD</span><span style="color:#ABB2BF;"> curl -f http://localhost:5000 || exit 1</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">CMD</span><span style="color:#ABB2BF;"> [</span><span style="color:#98C379;">&quot;flask&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&quot;run&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&quot;-h&quot;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&quot;0.0.0.0&quot;</span><span style="color:#ABB2BF;">]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><p>app.py：</p><div class="language-py line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">py</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#C678DD;">from</span><span style="color:#ABB2BF;"> flask </span><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> Flask</span></span>
<span class="line"><span style="color:#C678DD;">from</span><span style="color:#ABB2BF;"> redis </span><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> StrictRedis</span></span>
<span class="line"><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> os</span></span>
<span class="line"><span style="color:#C678DD;">import</span><span style="color:#ABB2BF;"> socket</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ABB2BF;">app </span><span style="color:#56B6C2;">=</span><span style="color:#61AFEF;"> Flask</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;">__name__</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#ABB2BF;">redis </span><span style="color:#56B6C2;">=</span><span style="color:#61AFEF;"> StrictRedis</span><span style="color:#ABB2BF;">(</span><span style="color:#E06C75;font-style:italic;">host</span><span style="color:#56B6C2;">=</span><span style="color:#ABB2BF;">os.environ.</span><span style="color:#61AFEF;">get</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;REDIS_HOST&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&#39;127.0.0.1&#39;</span><span style="color:#ABB2BF;">),</span></span>
<span class="line"><span style="color:#E06C75;font-style:italic;">                    port</span><span style="color:#56B6C2;">=</span><span style="color:#D19A66;">6379</span><span style="color:#ABB2BF;">, </span><span style="color:#E06C75;font-style:italic;">password</span><span style="color:#56B6C2;">=</span><span style="color:#ABB2BF;">os.environ.</span><span style="color:#61AFEF;">get</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;REDIS_PASS&#39;</span><span style="color:#ABB2BF;">))</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">@app</span><span style="color:#ABB2BF;">.</span><span style="color:#61AFEF;">route</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;/&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">def</span><span style="color:#61AFEF;"> hello</span><span style="color:#ABB2BF;">():</span></span>
<span class="line"><span style="color:#ABB2BF;">    redis.</span><span style="color:#61AFEF;">incr</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;hits&#39;</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#C678DD;">    return</span><span style="color:#C678DD;"> f</span><span style="color:#98C379;">&quot;Hello Container World! I have been seen </span><span style="color:#D19A66;">{</span><span style="color:#ABB2BF;">redis.</span><span style="color:#61AFEF;">get</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;hits&#39;</span><span style="color:#ABB2BF;">).</span><span style="color:#61AFEF;">decode</span><span style="color:#ABB2BF;">(</span><span style="color:#98C379;">&#39;utf-8&#39;</span><span style="color:#ABB2BF;">)</span><span style="color:#D19A66;">}</span><span style="color:#98C379;"> times and my hostname is </span><span style="color:#D19A66;">{</span><span style="color:#ABB2BF;">socket.</span><span style="color:#61AFEF;">gethostname</span><span style="color:#ABB2BF;">()</span><span style="color:#D19A66;">}</span><span style="color:#98C379;">.</span><span style="color:#56B6C2;">\n</span><span style="color:#98C379;">&quot;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h4 id="相关命令" tabindex="-1">相关命令 <a class="header-anchor" href="#相关命令" aria-label="Permalink to &quot;相关命令&quot;">​</a></h4><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># 启动容器</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> run</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --network</span><span style="color:#98C379;"> mybridge</span><span style="color:#D19A66;"> --env</span><span style="color:#98C379;"> REDIS_PASS=abc123</span><span style="color:#98C379;"> flask-demo</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 检查容器检查过程</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> inspect</span><span style="color:#D19A66;"> 70</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="完全演示" tabindex="-1">完全演示 <a class="header-anchor" href="#完全演示" aria-label="Permalink to &quot;完全演示&quot;">​</a></h3><div class="language-yaml line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#E06C75;">version</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">&#39;3.8&#39;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">services</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    flask</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        build</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">            context</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">./flask</span></span>
<span class="line"><span style="color:#E06C75;">            dockerfile</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">Dockerfile</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">flask-demo:latest</span></span>
<span class="line"><span style="color:#E06C75;">        environment</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">REDIS_HOST=redis-server</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">REDIS_PASS=${REDIS_PASSWORD}</span></span>
<span class="line"><span style="color:#E06C75;">        healthcheck</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">            test</span><span style="color:#ABB2BF;">: [</span><span style="color:#98C379;">&#39;CMD&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&#39;curl&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&#39;-f&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&#39;http://localhost:5000&#39;</span><span style="color:#ABB2BF;">]</span></span>
<span class="line"><span style="color:#E06C75;">            interval</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">30s</span></span>
<span class="line"><span style="color:#E06C75;">            timeout</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">3s</span></span>
<span class="line"><span style="color:#E06C75;">            retries</span><span style="color:#ABB2BF;">: </span><span style="color:#D19A66;">3</span></span>
<span class="line"><span style="color:#E06C75;">            start_period</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">40s</span></span>
<span class="line"><span style="color:#E06C75;">        depends_on</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">            redis-server</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">                condition</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">service_healthy</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">backend</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">frontend</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">    redis-server</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">redis:latest</span></span>
<span class="line"><span style="color:#E06C75;">        command</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">redis-server --requirepass ${REDIS_PASSWORD}</span></span>
<span class="line"><span style="color:#E06C75;">        healthcheck</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">            test</span><span style="color:#ABB2BF;">: [</span><span style="color:#98C379;">&#39;CMD&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&#39;redis-cli&#39;</span><span style="color:#ABB2BF;">, </span><span style="color:#98C379;">&#39;ping&#39;</span><span style="color:#ABB2BF;">]</span></span>
<span class="line"><span style="color:#E06C75;">            interval</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">1s</span></span>
<span class="line"><span style="color:#E06C75;">            timeout</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">3s</span></span>
<span class="line"><span style="color:#E06C75;">            retries</span><span style="color:#ABB2BF;">: </span><span style="color:#D19A66;">10</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">backend</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">    nginx</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">        image</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">nginx:stable-alpine</span></span>
<span class="line"><span style="color:#E06C75;">        ports</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">8000:80</span></span>
<span class="line"><span style="color:#E06C75;">        depends_on</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">            flask</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">                condition</span><span style="color:#ABB2BF;">: </span><span style="color:#98C379;">service_healthy</span></span>
<span class="line"><span style="color:#E06C75;">        volumes</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">./var/log/nginx:/var/log/nginx</span></span>
<span class="line"><span style="color:#E06C75;">        networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#ABB2BF;">            - </span><span style="color:#98C379;">frontend</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E06C75;">networks</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    backend</span><span style="color:#ABB2BF;">:</span></span>
<span class="line"><span style="color:#E06C75;">    frontend</span><span style="color:#ABB2BF;">:</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br></div></div><h4 id="相关命令-1" tabindex="-1">相关命令 <a class="header-anchor" href="#相关命令-1" aria-label="Permalink to &quot;相关命令&quot;">​</a></h4><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># 启动容器</span></span>
<span class="line"><span style="color:#61AFEF;">docker-compose</span><span style="color:#98C379;"> up</span><span style="color:#D19A66;"> -d</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-e257564d><!--[--><!--]--><div class="edit-info" data-v-e257564d><!----><div class="last-updated" data-v-e257564d><p class="VPLastUpdated" data-v-e257564d data-v-e98dd255>最后更新: <time datetime="2024-12-30T14:20:32.000Z" data-v-e98dd255></time></p></div></div><nav class="prev-next" aria-labelledby="doc-footer-aria-label" data-v-e257564d><span class="visually-hidden" id="doc-footer-aria-label" data-v-e257564d>Pager</span><div class="pager" data-v-e257564d><a class="VPLink link pager-link prev" href="/docker/Dockerfile.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>上一篇</span><span class="title" data-v-e257564d>Dockerfile</span><!--]--></a></div><div class="pager" data-v-e257564d><a class="VPLink link pager-link next" href="/docker/concept.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>下一篇</span><span class="title" data-v-e257564d>Docker 基础概念</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    
    
  </body>
</html>